﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using F23.StringSimilarity;

namespace EasyClassWEBAPI.Utils
{
    public class SimilarityComputation
    {
        /// <summary>
        /// The Jaro–Winkler distance metric is designed and best suited for 
        /// short strings such as person names, and to detect typos.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static double JaroWinklerMethod(string str1,string str2)
        {
            JaroWinkler jw = new JaroWinkler();
            return jw.Similarity(str1, str2);
        }
        /// <summary>
        /// The algorithm uses affixing with special character '\n' to increase the weight of first characters.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static double NGramMethod(string str1,string str2,int n)
        {
            NGram gram = new NGram(n);
            return gram.Distance(str1, str2);
        }
        /// <summary>
        /// Similar to Levenshtein, Damerau-Levenshtein distance with transposition 
        /// (also sometimes calls unrestricted Damerau-Levenshtein distance) 
        /// is the minimum number of operations needed to transform one string into the other,
        /// where an operation is defined as an insertion, deletion, or substitution of a single character, 
        /// or a transposition of two adjacent characters.
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static double DamerauLevenshteinMethod(string str1,string str2)
        {
            Damerau d = new Damerau();
            if (str1.Length >= str2.Length)
                return d.Distance(str1, str2);
            else
                return d.Distance(str2, str1);
        }
        /// <summary>
        /// 最长公共子距离
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public static double LCSMethod(string str1,string str2)
        {
            LongestCommonSubsequence lcs = new LongestCommonSubsequence();
            if (str1.Length >= str2.Length)
                return lcs.Distance(str1, str2);
            else
                return lcs.Distance(str2, str1);
        }
    }
}
